package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.inventory.domain.vo.ProductBomVo;
import com.ruoyi.inventory.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.ProductStruct;
import com.ruoyi.inventory.service.IProductStructService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 产品结构清单Controller
 * 
 * @author xwj
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/system/productStruct")
@Api(tags = "产品结构清单")
public class ProductStructController extends BaseController {

    @Autowired
    private IProductStructService productStructService;

    @Autowired
    private IProductService productService;


    /**
     * 产品BOM
     * @return
     */
    @GetMapping("/productBom")
    @ApiOperation("产品BOM")
    public AjaxResult productBom(Long productId) {
        List<ProductBomVo> bomList = productStructService.selectProductBom(productId);
        return success(bomList);
    }

    /**
     * 查询产品结构清单列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody ProductStruct productStruct) {
        System.out.println("#######2" + productStructService);
        startPage();
        List<ProductStruct> list = productStructService.selectProductStructList(productStruct);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody ProductStruct productStruct) {
        startOrderBy();
        List<ProductStruct> list = productStructService.selectProductStructList(productStruct);
        return success(list);
    }

    /**
     * 导出产品结构清单列表
     */
    @Log(title = "产品结构清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, ProductStruct productStruct) {
        List<ProductStruct> list = productStructService.selectProductStructList(productStruct);
        ExcelUtil<ProductStruct> util = new ExcelUtil<ProductStruct>(ProductStruct.class);
        util.exportExcel(response, list, "产品结构清单数据");
    }

    /**
     * 获取产品结构清单详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(productStructService.selectProductStructById(id));
    }

    /**
     * 新增产品结构清单
     */
    @Log(title = "产品结构清单", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody ProductStruct productStruct) {
        SysUser user = getLoginUser().getUser();
        if(productStruct.getId() == null) {
        productStruct.setCreateId(user.getUserId());
            productStruct.setCreateTime(DateUtils.getNowDate());
            productStruct.setCreateBy(user.getNickName());
            productStructService.insertProductStruct(productStruct);
        } else {
            productStruct.setUpdateId(user.getUserId());
            productStruct.setUpdateTime(DateUtils.getNowDate());
            productStruct.setUpdateBy(user.getNickName());
            productStructService.updateProductStruct(productStruct);
        }
        productService.updatePriceById(productStruct.getProductId(), productStruct.getAmount());
        return success(productStruct);
    }

    /**
     * 修改产品结构清单
     */
    @Log(title = "产品结构清单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody ProductStruct productStruct) {
        SysUser user = getLoginUser().getUser();
                productStruct.setUpdateId(user.getUserId());
                productStruct.setUpdateTime(DateUtils.getNowDate());
                productStruct.setUpdateBy(user.getNickName());
        return success(productStructService.updateProductStruct(productStruct));
    }

    /**
     * 删除产品结构清单
     */
    @Log(title = "产品结构清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productStructService.deleteProductStructByIds(ids));
    }

    @Log(title = "产品结构清单", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody ProductStruct productStruct) {
        int num = productStructService.delete(productStruct);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<ProductStruct>> queryPage(@RequestBody ProductStruct productStruct) {
        Pages<ProductStruct> page = productStructService.queryPage(productStruct);
        return ResponseModel.of(page);
    }
}
